package cn.yh.register.common.handler;

import cn.yh.register.common.exception.RegisterException;
import cn.yh.register.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    //处理其他类型的异常
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result<?> error(Exception e){
        e.printStackTrace();
        return Result.fail();
    }

    //处理自定义类型的异常
    @ExceptionHandler(value = RegisterException.class)
    @ResponseBody
    public Result<?> error(RegisterException e){
        e.printStackTrace();
        return Result.build(e.getCode(),e.getMessage());
    }
}
